package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    //分页查寻
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        List<Category> list = categoryMapper.pageQuery(categoryPageQueryDTO);
        Page<Category> page = (Page<Category>)list;
        return new PageResult(page.getTotal(),page.getResult());
    }
    //修改分类
    @Override
    public void update(CategoryDTO categoryDTO) {
        categoryMapper.update(categoryDTO);
    }
    //启用、禁用分类
    @Override
    public void startOrStop(Integer status, Long id) {
        categoryMapper.startOrStop(status , id);
    }

    @Override
    public void save(CategoryDTO categoryDTO) {
        Category category = new Category();
        category.setType(categoryDTO.getType());
        category.setName(categoryDTO.getName());
        category.setSort(categoryDTO.getSort());
        category.setStatus(StatusConstant.DISABLE);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(1L);
        category.setUpdateUser(1L);
        categoryMapper.save(category);
    }
   //根据id删除分类
    @Override
    public void deleteById(Long id) {
        Category category = categoryMapper.getById(id);
        if(category.getStatus() == StatusConstant.ENABLE){
            throw new DeletionNotAllowedException("启用中的分类不能删除");
        }
        List<Dish> byId = dishMapper.getByIds(id);
        if(byId != null && byId.size() > 0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        List<Setmeal> byId1 = setmealMapper.getByIds(id);
        if(byId1 != null && byId1.size() > 0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.deleteById(id);
    }
   //根据类型查询
    @Override
    public List<Category> selectList(Integer type) {
        return categoryMapper.selectList(type);
    }

}
